package com.sl.transport.mq;

import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.sl.transport.common.constant.Constants;

import com.sl.transport.common.exception.SLException;
import com.sl.transport.entity.node.AgencyEntity;
import com.sl.transport.entity.node.BaseEntity;
import com.sl.transport.entity.node.OLTEntity;
import com.sl.transport.entity.node.TLTEntity;
import com.sl.transport.enums.OrganTypeEnum;
import com.sl.transport.service.IService;
import com.sl.transport.utils.OrganServiceFactory;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

/**
 * 对于权限管家系统消息的处理
 */
@Slf4j
@Component
public class AuthMQListener {

    @RabbitListener(bindings = @QueueBinding(
            // 队列
            value = @Queue(name = Constants.MQ.Queues.AUTH_TRANSPORT),
            // 交换机
            exchange = @Exchange(name = "${rabbitmq.exchange}", type = ExchangeTypes.TOPIC),
            // routingKey
            key = "#"))
    public void listenAgencyMsg(String msg) {
        // {"type":"ORG","content":[{"managerId":"1","parentId":"0","name":"测试组织","id":"973902113476182273","status":true}],"operation":"UPDATE"}
        log.info("接收到权限管家机构变更消息：==> {}", msg);

        // 1.获取json对象
        JSONObject jsonObject = JSONUtil.parseObj(msg);
        // 2.先获取type
        String type = jsonObject.getStr("type");
        // 因为这里我们写死了只能传递过来org才能操作
        // 并且这里要进行return 不然抛异常rabbitmq就要进行重试，并在三次后存入数据库
        if (!StrUtil.equalsIgnoreCase(type, "ORG")) {
            log.info("接收到权限管家机构变更类型错误");
            return;
        }
        String operation = jsonObject.getStr("operation");
        // 因为content在当时存入的是数组 因此在转为json对象的时候要变为数组的对象
        JSONObject content = (JSONObject) jsonObject.getJSONArray("content").getObj(0);
        String name = content.getStr("name");
        Long parentId = content.getLong("parentId");

        // 3.根据操作不用的类型来获取对应的service
        IService iService;
        BaseEntity entity;
        // 4.根据里面的操作 来执行service里面的操作
        if (StrUtil.endWith(name, "转运中心")) {
            //一级转运中心
            iService = OrganServiceFactory.getBean(OrganTypeEnum.OLT.getCode());
            entity = new OLTEntity();
            // 没有父id
            entity.setParentId(0L);
        } else if (StrUtil.endWith(name, "分拣中心")) {
            //二级转运中心
            iService = OrganServiceFactory.getBean(OrganTypeEnum.TLT.getCode());
            entity = new TLTEntity();
            // 除了转运中心没有parentId其余的都有parentId
            entity.setParentId(parentId);
        } else if (StrUtil.endWith(name, "营业部")) {
            //网点
            iService = OrganServiceFactory.getBean(OrganTypeEnum.AGENCY.getCode());
            entity = new AgencyEntity();
            entity.setParentId(parentId);
        } else {
            return;
        }
        if (iService == null) {
            // 类型有误
            return;
        }

        // 设置参数
        entity.setBid(content.getLong("id"));
        entity.setName(name);
        entity.setStatus(content.getBool("status"));

        // 根据operation来判断是什么操作

        switch (operation) {
            case "ADD":
                iService.create(entity);
                break;
            case "UPDATE":
                iService.update(entity);
                break;
            case "DEL":
                iService.deleteByBid(entity.getBid());
                break;
        }


    }
}
